package com.chenqi.community.service.Impl;

import com.chenqi.community.dao.CommentDao;
import com.chenqi.community.entity.Comment;
import com.chenqi.community.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
/**
 * @author Ardai
 */
@Service
@Transactional(rollbackFor = {RuntimeException.class})
public class CommentServiceImpl implements CommentService {

    @Autowired
    private CommentDao commentDao;

    @Override
    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    public List<Comment> findCommentsByPostId(Integer pid) {
        return commentDao.findCommentsByPostId(pid);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = {RuntimeException.class})
    public void addComment(Comment comment) {
        comment.setCreateTime(new Date());
        commentDao.insertComment(comment);
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    public Integer findCommentsCount(Integer pid) {
        return commentDao.findCommentsCount(pid);
    }
}
